﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.BasicDataManage
{
    /// <summary>
    /// 异常类型数据层
    /// </summary>
    public class ETypeService : DbHelperBase
    {
        /// <summary>
        /// 获取异常类型绑定下拉列表
        /// </summary>
        /// <returns></returns>
        public List<ETypeEntity> GetETypeList()
        {
            var list = new List<ETypeEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT ETypeCode,ETypeName FROM UT_EType where RecStatus=1 Order by InputTime ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new ETypeEntity
                                            {
                                                ETypeCode =
                                                    reader["ETypeCode"] == obj
                                                        ? null
                                                        : Convert.ToString(reader["ETypeCode"]),
                                                ETypeName =
                                                    reader["ETypeName"] == obj
                                                        ? null
                                                        : Convert.ToString(reader["ETypeName"])
                                            };
                            list.Add(model);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        /// <summary>
        /// 添加异常类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddEType(ETypeEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO [UT_EType]([ETypeCode],[ETypeName],[ETypeDesc],RecStatus) ");
            strSql.Append(" VALUES(@ETypeCode,@ETypeName,@ETypeDesc,@RecStatus) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@ETypeCode", model.ETypeCode);
                    Db.AddInParameter(cmd, "@ETypeName", model.ETypeName);
                    Db.AddInParameter(cmd, "@ETypeDesc", model.ETypeDesc);
                    Db.AddInParameter(cmd, "@RecStatus", true);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        /// <summary>
        /// 修改异常类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool ModifyEType(ETypeEntity model)
        {

            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_EType SET [ETypeName] = @ETypeName,[ETypeDesc] = @ETypeDesc WHERE ETypeCode = @ETypeCode ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@ETypeName", model.ETypeName);
                    Db.AddInParameter(cmd, "@ETypeDesc", model.ETypeDesc);
                    Db.AddInParameter(cmd, "@ETypeCode", model.ETypeCode);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        /// <summary>
        /// 获取异常类型单条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ETypeEntity GetETypeInfo(string id)
        {
            var model = new ETypeEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT [ETypeCode],[ETypeName],[ETypeDesc],[RecStatus],[InputTime] FROM [UT_EType] where [ETypeCode]=@ETypeCode ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                Db.AddInParameter(cmd, "@ETypeCode", id);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            model.ETypeCode = reader["ETypeCode"] == obj ? null : Convert.ToString(reader["ETypeCode"]);
                            model.ETypeName = reader["ETypeName"] == obj ? null : Convert.ToString(reader["ETypeName"]);
                            model.ETypeDesc = reader["ETypeDesc"] == obj ? null : Convert.ToString(reader["ETypeDesc"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }
        /// <summary>
        /// 删除异常类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Remove(string id)
        {
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_EType Where ETypeCode=@ETypeCode ");
            var result = false;
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@ETypeCode", id);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public int GetETypeCount(ETypeQcEntity qcmodel)
        {
            var count = 0;
            var strSql = new StringBuilder();
            strSql.Append(" select  count(*) AS [count] FROM UT_EType AS A WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.RecStatus))
            {
                strSql.Append(" and RecStatus =" + qcmodel.RecStatus);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        /// <summary>
        /// 获取异常类型列表分页
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public List<ETypeEntity> GetETypeList(ETypeQcEntity qcmodel)
        {
            var list = new List<ETypeEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by InputTime) AS idx,ETypeCode,ETypeName,ETypeDesc,RecStatus FROM UT_EType where 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.RecStatus))
            {
                strSql.Append(" and RecStatus =" + qcmodel.RecStatus);
            }
            strSql.Append(" ) AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new ETypeEntity
                                            {
                                                ETypeCode =
                                                    reader["ETypeCode"] == obj
                                                        ? null
                                                        : Convert.ToString(reader["ETypeCode"]),
                                                ETypeName =
                                                    reader["ETypeName"] == obj
                                                        ? null
                                                        : Convert.ToString(reader["ETypeName"]),
                                                ETypeDesc =
                                                    reader["ETypeDesc"] == obj
                                                        ? null
                                                        : Convert.ToString(reader["ETypeDesc"])
                                            };
                            if (reader["RecStatus"] != DBNull.Value)
                                model.RecStatus = Convert.ToBoolean(reader["RecStatus"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
    }
}
